package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.dao.PictureDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.PicturePO;

@Controller
public class PictureAction {
	@Autowired
	private PictureDao pictureDao;
	private PicturePO picturePO;
	private List<PicturePO> picturePOs;
	private String imageFileName;
	private File image;
	
	private String pictureName;
	
	private String intro;
	/**
	 * 展示实训图
	 * @return
	 */
	public String showInfo() {
		picturePOs = pictureDao.findPictureList();
		return "show";
	}
	
	/**
	 * 修改数据时回显数据
	 * @return
	 */
	public String preUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String id = request.getParameter("id");
		picturePO = pictureDao.findPicturePOById(id);
		ActionContext.getContext().put("id", id);
		return "preUpdate";
	}
	
	/**
	 * 修改数据
	 * @return
	 */
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		Map<String, String> map = new HashMap<String, String>();
		Map<String, String> map2 = new HashMap<String, String>();
		String id = request.getParameter("id");
		map.put("id", id);
		map.put("imageName", pictureName);
		map.put("intro", intro);
		
		try {
			pictureDao.updateInfo(map);
			map2.put("code", "EC001");
		} catch (Exception e) {
			map2.put("code", "EC002");
		}
		try {
			String msg = JSON.toJSONString(map2);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	/**
	 * 上传图片
	 */
	public String upImage(){
		try {
			//给上传的图片重新起名字
			String name = imageFileName;
			long date = System.currentTimeMillis();
			String NewName = date + name.substring(name.lastIndexOf(".")); 
			
			//获取ServletContext对象
			ServletContext context = ServletActionContext.getServletContext();
			
			//	给服务器中上传文件
			FileInputStream fis = new FileInputStream(image);
			int t = 0;
			byte[] bys = new byte[1024];
			FileOutputStream fos = new FileOutputStream(context.getRealPath("/") + "picture_image/" + NewName);
			while((t = fis.read(bys)) != -1){
				fos.write(bys, 0, t);
			}
			//关闭流
			fis.close();
			fos.close();
			//服务器响应的数据
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(NewName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 return null;
	}
	
	
	
	
	public String getPictureName() {
		return pictureName;
	}

	public void setPictureName(String pictureName) {
		this.pictureName = pictureName;
	}


	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public List<PicturePO> getPicturePOs() {
		return picturePOs;
	}

	public void setPicturePOs(List<PicturePO> picturePOs) {
		this.picturePOs = picturePOs;
	}

	public PicturePO getPicturePO() {
		return picturePO;
	}

	public void setPicturePO(PicturePO picturePO) {
		this.picturePO = picturePO;
	}
	
}
